//
//  EFGoodsCategoryView.m
//  EnjogFood
//
//  Created by guocheng on 2017/6/1.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFGoodsCategoryView.h"
#import "EFGoodDetailViewItemGuessLike.h"
#import "iToast.h"

@interface EFGoodsCategoryView()<UITableViewDelegate, UITableViewDataSource>

@property(strong,nonatomic)UITableView* tableView;
@property(nonatomic, strong)NSMutableArray *dataArray;
@property(nonatomic, strong)UIImageView *topIV;
@property (nonatomic, strong) MBProgressHUD *HUD;
@property(nonatomic,strong)NSString* categoryId;
@end

@implementation EFGoodsCategoryView

-(instancetype)initWithFrame:(CGRect)frame categoryId:(NSString *)cateroryId{
    self = [super initWithFrame:frame];
    if(self){
        self.categoryId = cateroryId;
        self.dataArray = [NSMutableArray array];
       
        self.page = 1;
        self.size = 20;
        //[self initData];
         __weak typeof(self) weakSelf = self;
        [self.tableView addLegendHeaderWithRefreshingBlock:^{
            weakSelf.page = 0;
            [weakSelf.dataArray removeAllObjects];
            [weakSelf initData];
        }];
        [self.tableView addLegendFooterWithRefreshingBlock:^{
            weakSelf.page ++;
            [weakSelf initData];
        }];
         [self initView];
    }
    return self;
}

-(void)initView{
    [self addSubview:self.topIV];
    [self addSubview:self.tableView];
    self.tableView.hidden = YES;
    self.tableView.footer.hidden = YES;
    [self setHUD:[[MBProgressHUD alloc] initWithView:self]];
}

- (void)setHUD:(MBProgressHUD *)theHUD {
    [_HUD removeFromSuperview];
    _HUD = theHUD;
    [self addSubview:_HUD];
}

- (void)showHUD {
    [self bringSubviewToFront:self.HUD];
    [self.HUD show:YES];
}

-(void)initData{
    [self showHUD];
    __weak typeof(self) weakSelf = self;
    [APP_CONTEXT.net getProductCategory:self.categoryId page:[NSString stringWithFormat:@"%ld",self.page] size:[NSString stringWithFormat:@"%ld",(long)self.size] OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        weakSelf.HUD.hidden = YES;
        weakSelf.tableView.hidden = NO;
        weakSelf.tableView.footer.hidden = NO;
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
        if (self.page == 1) {
            [weakSelf.dataArray removeAllObjects];
        }
        if(code == 0){
            NSString* topImg = [info objectForKey:@"categoryImg"];
                [weakSelf.topIV setImageWithURL:[NSURL URLWithString:topImg]];
                NSArray *array = [info objectForKey:@"productList"];
            if (array.count == 0) {
                weakSelf.tableView.footer.hidden = YES;
            }else{
                weakSelf.tableView.footer.hidden = NO;
                for(int i =0 ; i < array.count; ++i){
                    EFGoodDetailViewItemGuessLike* guestItem = [[EFGoodDetailViewItemGuessLike alloc]init];
                    NSDictionary *leftDic = [array objectAtIndex:i];
                    EFSepecialItem* leftItem = [EFSepecialItem yy_modelWithDictionary:leftDic];
                    guestItem.leftItem = leftItem;
                    
                    int j = i+1;
                    if(j < array.count){
                        NSDictionary *rightDic = [array objectAtIndex:j];
                        EFSepecialItem* rightItem = [EFSepecialItem yy_modelWithDictionary:rightDic];
                        guestItem.rightItem = rightItem;
                        i= j;
                    }else{
                        guestItem.rightItem = NULL;
                    }
                    
                    [weakSelf.dataArray addObject:guestItem];
                }

            }
            [weakSelf.tableView reloadData];
        }else{
            [[iToast makeText:message]show];
        }
    }];
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ICTableViewItem *item = self.dataArray[indexPath.row];
    
    return item.viewHeight;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
    if (self.dataArray.count > 0) {
        ICTableViewItem* item = self.dataArray[indexPath.row];
        NSString *identifier = NSStringFromClass(item.viewClass);
        cell = (ICTableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil){
            cell = [[item.viewClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        if ([item isKindOfClass:[ICTableViewItem class]]) {
            [(ICTableViewCell *)cell setObject:item];
        }

    }
        return cell;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 164, kScreenWidth, self.height - 164 )];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.top = self.topIV.bottom;
    }
    return _tableView;
}

-(UIImageView *)topIV{
    if(_topIV == NULL){
        _topIV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 164)];
        _topIV.backgroundColor = RGBCOLOR(0xf3f3f3);
        _topIV.contentMode = UIViewContentModeScaleAspectFill;
        _topIV.clipsToBounds = YES;
    }
    return _topIV;
}

@end
